<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--用户数据-->
      <el-col :span="mainwidth" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

          <!--任务类别   -->
          <el-form-item label="抄送状态" prop="status">
            <el-select
              v-model="queryParams.cc_state"
              placeholder="抄送状态"
              clearable
              size="small"
              style="width: 110px"
            >
              <el-option
                v-for="dict in CCstatus"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>

          <!--抄送结果   -->
          <el-form-item label="抄送结果" prop="status">
            <el-select
              v-model="queryParams.cc_result"
              placeholder="抄送结果"
              clearable
              size="small"
              style="width: 110px"
            >
              <el-option
                v-for="dict in Cchis_result"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>

          <!--  时间类型-->
          <el-form-item label="时间类型" prop="status">
            <el-select
              v-model="queryParams.timetype"
              placeholder="时间类型"
              clearable
              size="small"
              style="width: 110px"
            >
              <el-option
                v-for="dict in timetypeHis"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-date-picker
              v-model="selTime"
              type="datetimerange"
              :picker-options="pickerOptions"
              range-separator="至"
              value-format="yyyy-MM-dd"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              style="width: 360px"
              align="right">
            </el-date-picker>

          </el-form-item>

        </el-form>

        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px" style="margin-bottom: 10px">
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-input
                v-model="queryParams.value"
                placeholder="查询值"
                clearable
                size="small"
                style="width: 350px"
                @keyup.enter.native="handleQuery"
              >
                <el-select
                  v-model="queryParams.type"
                  placeholder="查询条件"
                  clearable
                  slot="prepend"
                  style="width: 110px"
                >
                  <el-option
                    v-for="dict in typeOptionsCcUrl"
                    :key="dict.dictValue"
                    :label="dict.dictLabel"
                    :value="dict.dictValue"
                  />
                </el-select>
              </el-input>
            </el-col>
            <el-col :span="1.5">
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          </el-row>
        </el-form>

        <el-table v-loading="loading" :data="cardList" @selection-change="handleSelectionChange">
          <el-table-column label="序号" align="center" prop="id" v-if="columns[0].visible"/>
          <el-table-column label="二维码" align="center" prop="qrcode" v-if="columns[1].visible"/>
          <el-table-column label="IMEI" align="center" prop="imei" v-if="columns[2].visible"/>
          <el-table-column label="状态" align="center" prop="state" v-if="columns[3].visible"/>
          <el-table-column label="类型-型号" align="center" prop="type" v-if="columns[4].visible"/>
          <el-table-column label="注册日期" align="center" prop="date" v-if="columns[5].visible"/>
          <el-table-column label="公司所属" align="center" prop="dept_id" v-if="columns[6].visible"/>
          <el-table-column label="安装点位" align="center" prop="mounting" v-if="columns[8].visible">
            <template slot-scope="scope" >
              <el-button type="primary" @click="Binding(scope.row)" size="mini" v-if="scope.row.mounting == 0">绑定</el-button>
              <router-link :to="'/installpoint/installpoint/index'+ scope.row.id" class="link-type" v-model="form.install_name" v-if="scope.row.mounting == 1">
                {{scope.row.dept_id}}
              </router-link>
            </template>

          </el-table-column>
          <el-table-column label="安装人员" align="center" prop="Installer" v-if="columns[9].visible"/>
          <el-table-column label="安装人员电话" align="center" prop="mountingphon" v-if="columns[9].visible">

          </el-table-column>
          <el-table-column label="信号" align="center" prop="signal" v-if="columns[9].visible"/>
          <el-table-column label="模式" align="center" prop="pattern" v-if="columns[9].visible"/>
          <el-table-column label="批次" align="center" prop="batch" v-if="columns[9].visible"/>
          <el-table-column label="最近事件时间" align="center" prop="last_time" v-if="columns[9].visible"/>


        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!--绑定按钮-->
    <el-dialog title="绑定安装点" :visible.sync="open" width="800px" append-to-body>
      <div>
        <span style="margin-left: 70px">IMEI<span style="margin-left: 15px;color: blue">{{ form.imeiDeptId }}</span></span>
      </div>
      <br>
      <div>
        <span style="margin-left: 38px">类型-型号<span style="margin-left: 15px;color: blue">{{ form.magnetism }}</span></span>
      </div>
      <br><br><br>
      <el-form :model="form">
        <el-form-item label="单位" :required="true" style="margin-left: 60px">
          <el-input placeholder="请输入" v-model="form.dept_id" style="width: 300px" :disabled="true"/>
          <el-button type="primary" style="margin-left: 10px" size="small">选择</el-button>
          <el-button type="primary" style="margin-left: 10px" icon="el-icon-refresh-left" size="small" circle/>
        </el-form-item>
        <br>
        <el-form-item label="建筑物" style="margin-left: 55px">
          <el-select
            v-model="form.building_name"
            placeholder="请选择建筑物"
            clearable
            @change="detailsSle"
            size="small"
            style="width: 330px"
          >
            <el-option
              v-for="dict in SleBuilding"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
          <el-button type="primary" style="margin-left: 10px" icon="el-icon-refresh-left" size="small" circle/>
          <el-button type="primary" style="margin-left: 10px" icon="el-icon-plus" size="small" circle/>
        </el-form-item>
        <br>
        <el-form-item label="楼层" style="margin-left: 70px">
          <el-select
            :disabled="choice"
            v-model="form.y_floor"
            placeholder="请选择"
            clearable
            size="small"
            style="width: 330px"
          >
            <el-option
              v-for="dict in SleFloor"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
          <el-button type="primary" style="margin-left: 10px" icon="el-icon-refresh-left" size="small" circle/>
          <el-button type="primary" style="margin-left: 10px" icon="el-icon-plus" size="small" circle/>
        </el-form-item>
        <br>
        <el-form-item label="地图标记" :required="true" label-width="100px">
          <el-button type="primary" style="margin-left: 10px" size="small">精确选择</el-button>
        </el-form-item>
        <br>
        <el-form-item label="详情地址"  :required="true" label-width="110px">
          <el-input placeholder="请输入" v-model="form.address" style="width: 330px"/>
        </el-form-item>
        <br>
        <el-form-item label="安装点名称" :required="true" label-width="110px">
          <el-input placeholder="请输入安装点名称" v-model="form.install_name" style="width: 330px"/>
        </el-form-item>
        <br>
        <el-form-item label="接警联系人" style="margin-left: 20px" :required="true">
          <el-select
            v-model="form.name"
            placeholder="请选择接警联系人"
            clearable
            multiple
            size="small"
            style="width: 330px"
          >
            <el-option
              v-for="dict in SleAlarm"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
          <el-button type="primary" style="margin-left: 10px" icon="el-icon-refresh-left" size="small" circle/>
          <el-button type="primary" style="margin-left: 10px" icon="el-icon-plus" size="small" circle/>
          <el-button type="primary" style="margin-left: 10px" icon="el-icon-s-tools" size="small" circle/>
        </el-form-item>
        <br>
        <el-form-item label="住户资料" style="margin-left: 45px">
          <el-select
            v-model="form.household"
            placeholder="请选择住户资料"
            clearable
            multiple
            size="small"
            style="width: 330px"
          >
            <el-option
              v-for="dict in SleHousehold"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
          <el-button type="primary" style="margin-left: 10px" icon="el-icon-refresh-left" size="small" circle/>
          <el-button type="primary" style="margin-left: 10px" icon="el-icon-plus" size="small" circle/>
        </el-form-item>
      </el-form>
      <br><br><br>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import {listEquipment, SelAddKey, SleBuildingAdd,SleSleFloorAdd,SlesleAlarmAdd,AddEquipment,
        SleHouseholdAdd} from "@/api/yunze/install/equipment";
import tools from "@/utils/yunze/tools";

export default {
  name: "index",
  data() {
    return {

      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      tools: tools,
      SleBuilding: [],//建筑物
      SleAlarm: [],//接警联系人
      SleHousehold: [],//住户资料
      restaurants: [],
      state1: '',
      selTime: '',//时间选择
      mainwidth: 24,//宽度
      choice: true,//楼层空值禁止
      SleFloor: [],//楼层
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 用户表格数据
      cardList: null,
      // 是否显示弹出层
      open: false,
      // 时间数组
      timetypeHis: [],
      //抄送状态
      CCstatus: [],
      // 抄送结果
      Cchis_result: [],
      // 查询条件
      typeOptionsCcUrl: [],
      // 表单参数
      form: {
        dept_id: null,
        address: '',
        install_name: '',
        imeiDeptId: '',//IMEI和公司名称
        magnetism: '',//门磁
        create_time: tools.gitData(),
        Installer: null,
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 列信息
      columns: [
        {key: 1, label: `抄送任务id`, visible: true},
        {key: 2, label: `推送API`, visible: true},
        {key: 3, label: `推送邮件`, visible: true},
        {key: 4, label: `抄送状态`, visible: true},
        {key: 5, label: `抄送结果`, visible: true},
        {key: 6, label: `抄送次数`, visible: true},
        {key: 7, label: `备注`, visible: true},
        {key: 8, label: `抄送参数`, visible: true},
        {key: 9, label: `创建时间`, visible: true},
        {key: 10, label: `最近一次抄送时间`, visible: true},
      ],
      // 表单校验
      rules: {}
    };
  },

  created() {


    //加载 查询条件
    if (window['typeOptionsCcUrl'] != undefined && window['typeOptionsCcUrl'] != null && window['typeOptionsCcUrl'] != '') {
      this.typeOptionsCcUrl = window['typeOptionsCcUrl'];
    } else {
      this.getDicts("yunze_typeOptionsCcUrl").then(response => {
        window['typeOptionsCcUrl'] = response.data;
        this.typeOptionsCcUrl = window['typeOptionsCcUrl'];
      });
    }

    //加载 抄送状态
    if (window['CCstatus'] != undefined && window['CCstatus'] != null && window['CCstatus'] != '') {
      this.CCstatus = window['CCstatus'];
    } else {
      this.getDicts("yz_cc_HisStatus").then(response => {
        window['CCstatus'] = response.data;
        this.CCstatus = window['CCstatus'];
      });
    }

    //加载 抄送结果
    if (window['Cchis_result'] != undefined && window['Cchis_result'] != null && window['Cchis_result'] != '') {
      this.Cchis_result = window['Cchis_result'];
    } else {
      this.getDicts("yz_cc_result").then(response => {
        window['Cchis_result'] = response.data;
        this.Cchis_result = window['Cchis_result'];
      });
    }

    //加载 时间赛选查询条件
    if (window['timetypeHis'] != undefined && window['timetypeHis'] != null && window['timetypeHis'] != '') {
      this.timetypeHis = window['timetypeHis'];
    } else {
      this.getDicts("yunze_CcHis_timetype").then(response => {
        window['timetypeHis'] = response.data;
        this.timetypeHis = window['timetypeHis'];
      });
    }


    this.getList();
  },
  methods: {

    Name(row){
      let map = {}
      map.dept_id = row.dept_id;
      let Pwd_Str = tools.encrypt(JSON.stringify(map));
      SelAddKey(Pwd_Str).then(response => {
        // console.log(response)
        let jsonobj = JSON.parse(tools.Decrypt(response));
        console.log(jsonobj.Data)
        for (let i = 0; i < jsonobj.Data.length; i++) {
          let obj = jsonobj.Data[i];
          this.form.address = obj.address;
          this.form.install_name = obj.building_name;
        }
      })
      // console.log(row)
      // console.log(this.form.install_name)
    },

    /**绑定事件*/
    Binding(row) {
      this.open = true;
      this.form.imeiDeptId = row.imei;
      this.form.magnetism = row.type;
      this.form.dept_id = Number(row.dept_id);
      this.form.Installer = row.Installer;
      // console.log(row)
      // console.log(this.form.dept_id)

      //接警联系人
      if (window['SleAlarm'] != undefined && window['SleAlarm'] != null && window['SleAlarm'] != '') {
        this.SleAlarm = window['SleAlarm'];
      } else {
        let Pwd_Str = tools.encrypt(JSON.stringify(this.form));
        SlesleAlarmAdd(Pwd_Str).then(response => {
          // console.log(response)
          let jsonobj = JSON.parse(tools.Decrypt(response));
            window['SleAlarm'] = jsonobj.Data;
            // console.log(jsonobj.Data)
            this.SleAlarm = window['SleAlarm'];
        });
      }

      //住户资料
      if (window['SleHousehold'] != undefined && window['SleHousehold'] != null && window['SleHousehold'] != '') {
        this.SleHousehold = window['SleHousehold'];
      } else {
        let Pwd_Str = tools.encrypt(JSON.stringify(this.form));
        SleHouseholdAdd(Pwd_Str).then(response => {
          // console.log(response)
          let jsonobj = JSON.parse(tools.Decrypt(response));
            window['SleHousehold'] = jsonobj.Data;
            // console.log(jsonobj.Data)
            this.SleHousehold = window['SleHousehold'];
        });
      }

      //查询dept_id下的建筑物名称  SleBuilding
      if (window['SleBuilding'] != undefined && window['SleBuilding'] != null && window['SleBuilding'] != '') {
        this.SleBuilding = window['SleBuilding'];
      } else {
        let Pwd_Str = tools.encrypt(JSON.stringify(this.form));
        SleBuildingAdd(Pwd_Str).then(response => {
          // console.log(response)
          let jsonobj = JSON.parse(tools.Decrypt(response));
          window['SleBuilding'] = jsonobj.Data;
          // console.log(jsonobj.Data)
          this.SleBuilding = window['SleBuilding'];
        });
      }
    },

    //查询建筑物详情
    detailsSle(){
      this.choice = false;
      if (window['SleFloor'] != undefined && window['SleFloor'] != null && window['SleFloor'] != '') {
        this.SleFloor = window['SleFloor'];
      } else {
        let Pwd_Str = tools.encrypt(JSON.stringify(this.form));
        SleSleFloorAdd(Pwd_Str).then(response => {
          // console.log(response)
          let jsonobj = JSON.parse(tools.Decrypt(response));
          window['SleFloor'] = jsonobj.Data;
          // console.log(jsonobj.Data)
          this.SleFloor = window['SleFloor'];
        });
      }
      // console.log(this.form.building_name)
      let Pwd_Str = tools.encrypt(JSON.stringify(this.form));
      SelAddKey(Pwd_Str).then(response => {
        // console.log(response)
        let jsonobj = JSON.parse(tools.Decrypt(response));
        // console.log(jsonobj.Data)
        for (let i = 0; i < jsonobj.Data.length; i++) {
          let obj = jsonobj.Data[i];
          this.form.address = obj.address;
          this.form.install_name = obj.building_name;
        }
      })
    },


    /**新增确定按钮*/
    submitForm(){
      let _this = this;
      if (tools.VerificationsText(_this, _this.form.address, "详细地址不能为空！") == true &&
        tools.VerificationsText(_this, _this.form.install_name, "安装点名称不能为空！") == true &&
        tools.VerificationsText(_this, _this.form.name, "接警联系人不能为空！") == true
      ){
        let Pwd_Str = tools.encrypt(JSON.stringify(this.form));
        AddEquipment(Pwd_Str).then(response => {
          let jsonobj = JSON.parse(tools.Decrypt(response));
          //console.log(jsonobj);
          if (jsonobj.code == 200) {
            this.msgSuccess(jsonobj.msg);
            this.open = false;

            this.handleQuery();
          } else {
            this.msgError("获取数据异常，请联系管理员！");
          }
        });
      }

    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.jobId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },

    /*获取查询参数*/
    getParams() {

      if (this.$refs.dept != undefined & this.$refs.dept != null & this.$refs.dept != 'undefined') {
        //console.log(this.$refs.dept.getCheckedKeys());
        if (this.$refs.dept.getCheckedKeys().length > 0) {
          this.queryParams.agent_id = this.$refs.dept.getCheckedKeys();
        }
      }
      if (tools.Is_null(this.queryParams.timetype) && this.selTime != null) {
        //console.log(this.selTime);
        this.queryParams.staTime = this.selTime[0];
        this.queryParams.endTime = this.selTime[1];
      }


    },


    /** 查询执行日志列表 */
    getList() {
      this.loading = true;
      this.getParams();
      this.queryParams.page = 1;
      // console.log(this.queryParams);
      let Pwd_Str = tools.encrypt(JSON.stringify(this.queryParams));
      //console.log(Pwd_Str);
      listEquipment(Pwd_Str).then(response => {
          let jsonobj = JSON.parse(tools.Decrypt(response));
          // console.log(jsonobj);
          if (jsonobj.code == 200) {
            this.cardList = jsonobj.Data.Data;
            this.total = jsonobj.Data.Pu.rowCount;
          } else {
            this.msgError("获取数据异常，请联系管理员！");
          }
          this.loading = false;
        }
      );
    },


    // 取消按钮
    cancel() {
      this.open = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {};
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 10;
      this.selTime = null;
      this.handleQuery();
    },
  },

};
</script>

<style scoped>


</style>

